<?php


namespace app\common\model;


use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class RechargeLog extends Model
{

// 表名
    protected $name = 'recharge_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'recharge_type_text'
    ];

    public function createRecharge($data)
    {
        try {
            $recharge = self::create($data);
            return $recharge;
        } catch (Exception $exception) {
            return null;
        }

    }

    public function getList($user_id, $page = 1, $extend = [])
    {
        $pageSize = 20;
        $start = ($page - 1) * $pageSize;
        $where = ['user_id' => $user_id];
        $where = array_merge($where, $extend);
        $list = $this->where($where)->limit($start, $pageSize)->order("id", "desc")->select();
        return $list ?? [];
    }

    public function getRechargeTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recharge_type']) ? $data['recharge_type'] : '');
        $list = [1 => '微信充值', 2 => '管理员充值'];
        return isset($list[$value]) ? $list[$value] : '';
    }

}